from PyQt5.QtCore import *
from PyQt5.QtGui import QFocusEvent
from examples.example_calculator.calc_conf import *#--
from examples.example_calculator.calc_node_base import *#--
from nodeeditor.utils import dumpException
import examples.example_calculator.tuoZhan as tuoZhan

#---------------------------------------------
class CalcGraphicsNode(QDMGraphicsNode):#节点外壳
    def initSizes(self):
        super().initSizes()
        self.width = 220
        self.height = 85
        self.edge_roundness = 6
        self.edge_padding = 0
        self.title_horizontal_padding = 8
        self.title_vertical_padding = 10

    def initAssets(self):
        super().initAssets()
        self.icons = QImage("icons/status_icons.png")

    def paint(self, painter, QStyleOptionGraphicsItem, widget=None):
        super().paint(painter, QStyleOptionGraphicsItem, widget)

        offset = 24.0
        if self.node.isDirty(): offset = 0.0
        if self.node.isInvalid(): offset = 48.0

        painter.drawImage(
            QRectF(-10, -10, 24.0, 24.0),
            self.icons,
            QRectF(offset, 0, 24.0, 24.0)
        )
#---------------------------------------------
class CalcChongDingXiangContent(QDMNodeContentWidget):
    def initUI(self):
        self.edit = QLineEdit("0000", self)#此框输入任务编号
        self.edit.setAlignment(Qt.AlignLeft)
        #---------------------------------------
        self.edit2 = QLineEdit("0", self)#此框输入资源编号
        self.edit2.setAlignment(Qt.AlignLeft)
        #---------------------------------------
        self.button = QPushButton(text="开启",parent=self)
        self.button2 = QPushButton(text="完成",parent=self)
        #---------------------------------------
        self.layout1 = QHBoxLayout()#横框
        self.layout1.addWidget(self.edit)
        self.layout1.addWidget(self.edit2)
        self.layout2 = QHBoxLayout()#横框
        self.layout2.addWidget(self.button)
        self.layout2.addWidget(self.button2)
        #---------------------------------------
        self.label = QLabel(self)
        #---------------------------------------
        self.layout = QVBoxLayout()#竖框
        self.layout.setContentsMargins(10,0,10,0)
        self.setLayout(self.layout)
        self.layout.addLayout(self.layout1)
        self.layout.addLayout(self.layout2)
        self.layout.addWidget(self.label)
        #---------------------------------------
        
    def openimage(self,name01,name02):
        imgName = "./Task/" + name01 + "/" + name02 + ".bmp"
        print("图**********")
        print(imgName)
        jpg = QPixmap(imgName)
        self.label.width = jpg.width()
        self.label.height = jpg.height()
        self.label.setPixmap(jpg)
    
    def serialize(self):
        res = super().serialize()
        res['value'] = self.edit.text()#任务编号
        res['value2'] = self.edit2.text()#资源编号
        return res

    def deserialize(self, data, hashmap={}):
        res = super().deserialize(data, hashmap)
        try:
            value = data['value']
            value2 = data['value2']
            self.edit.setText(value)
            self.edit2.setText(value2)
            return True & res
        except Exception as e:
            dumpException(e)
        return res

#---------------------------------------------
@register_node(OP_NODE_ChongDingXiang)
class CalcNode_ChongDingXiang(CalcNode):
    icon = "icons/add.png"
    op_code = OP_NODE_ChongDingXiang
    op_title = "重定向资源"
    content_label_objname = "calc_node_ChongDingXiang"
    GraphicsNode_class = CalcGraphicsNode

    def __init__(self, scene):
        super().__init__(scene, inputs=[], outputs=[])
        self.eval()
        
        self.time02 = QTimer()
        self.time02.timeout.connect(self.time03)
        self.time02.setInterval(2000)
        self.time02.start()

    def initInnerClasses(self):
        self.content = CalcChongDingXiangContent(self)
        self.grNode = CalcGraphicsNode(self)
        self.content.edit.textChanged.connect(self.onInputChanged)
        self.content.edit2.textChanged.connect(self.onInputChanged)
        self.content.button.clicked.connect(self.kaiQi)#开启
        self.content.button2.clicked.connect(self.wanCheng)#完成
    
    def onInputChanged(self, socket=None):
        super().onInputChanged(socket)
        #---------------------------------
        self.startSetImage()
        #---------------------------------
        self.grNode.height = 25 + 25 + 25 + self.content.label.size().height()
        self.grNode.initContent()
    
    def evalImplementation(self):
        s_value = self.content.edit.text()
        self.value = s_value
        self.markDirty(False)
        self.markInvalid(False)
        self.markDescendantsInvalid(False)
        self.markDescendantsDirty()
        self.grNode.setToolTip("")
        self.evalChildren()
        return self.value

    def kaiQi(self):
        #开启--在绑定的窗口中创建图片
        text01 = self.content.edit.text()
        text02 = self.content.edit2.text()
        tuoZhan.kaiQi(text01,text01 + '_' + text02)
        pass
    
    def wanCheng(self):
        #完成--根据坐标截图，并且覆盖之前的数据
        text01 = self.content.edit.text()
        text02 = self.content.edit2.text()
        tuoZhan.wanCheng(text01,text01 + '_' + text02)
        self.startSetImage()#尝试恢复图片
        pass
    
    def time01(self):
        #print("延迟1秒")
        self.time02.stop()
        self.grNode.height = 25 + 25 + 25 + self.content.label.height
        if(self.content.label.width <= 220):
            self.grNode.width = 220
        else:
            self.grNode.width = self.content.label.width
        self.grNode.initContent()
        
    def time03(self):
        #print("延迟1秒")
        self.time02.stop()
        self.startSetImage()#尝试恢复图片
    
    def startSetImage(self):
        #开始时设置图片
        str01 = self.content.edit.text()#任务编号
        str02 = self.content.edit2.text()#资源编号
        self.content.openimage(str01,str01 + '_' + str02)
        #---------------------------------------------------------
        self.grNode.height = 25 + 25 + 25 + self.content.label.height
        if(self.content.label.width <= 220):
            self.grNode.width = 220
        else:
            self.grNode.width = self.content.label.width
        self.grNode.initContent()
        self.time02 = QTimer()
        self.time02.timeout.connect(self.time01)
        self.time02.setInterval(500)
        self.time02.start()